import { axios } from "../../utils/request"

Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo:{},
    showShare: false,
    options: [
      { name: '微信', icon: 'wechat', openType: 'share' },
      { name: '微博', icon: 'weibo' },
      { name: '复制链接', icon: 'link' },
      { name: '分享海报', icon: 'poster' },
      { name: '二维码', icon: 'qrcode' },
    ],
  },
  login(){
    return new Promise((resolve,reject)=>{
      wx.login({
        success(res){
          resolve(res)
        },
        fail(err){
          reject(err)
        }
     })
    })
  },
  async getUserInfo(){
    //
    const getInfo = ()=> wx.getUserProfile({desc:"注册登录"})
    const [info,loginData] =  await Promise.all([getInfo(),this.login()])
    const code = (loginData as {code:string}).code
    const userInfo = info.userInfo
    let UserData = {code,...userInfo}
    let res =  await axios("/user/login","POST",{...UserData}) as any
    if(res.statusCode==200||res.data.statusCode==200){
      let token = res.data.data[0].openId
      let userInfo = res.data.data[0]
      wx.setStorageSync('token',token)
      wx.setStorageSync('userInfo',userInfo)
      wx.setStorageSync('isLogin',true)
      this.setData({
        userInfo
      })
    }
  },
  logout(){
    wx.removeStorageSync("token")
    wx.removeStorageSync("userInfo")
    wx.removeStorageSync("isLogin")
    this.setData({
        userInfo:"" as any
    })
  },
  editorInfo(){
    wx.navigateTo({
      url:"/pages/userEditor/userEditor"
    })
  },
  toStart(){
    wx.navigateTo({
      url:'/pages/start/start'
    })
  },
  showContact(){
    this.setData({
      showContact:true
    })
  },
  toShow(){
    this.setData({
        isShow:true
    })
  },
  contactClose(){
    this.setData({
      showContact:false,
      isShow:false
    })
  },
  onShare() {
    this.setData({ showShare: true });
  },
  shareClose() {
    this.setData({ showShare: false });
  },
  copy(){
    wx.setClipboardData({
      data: 'chengyi@163.com',
      success: ()=> {
        wx.getClipboardData({
          success: () =>{
            wx.showToast({
              title: '复制成功'
            })
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getTabBar().setData({
      active:4
    })
    const token = wx.getStorageSync("token")
    if(token){
      const userInfo = wx.getStorageSync("userInfo")
      this.setData({
        userInfo
      })
    }
  }
})